package com.montran.search;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.exceptions.FilterException;
import com.exceptions.ResourcesException;
import com.montran.read.resources.UtilPropertyFiles;
import com.montran.sort.News;

public class ManagerSearch {
	String dateFormat = new String();
	{
		try {
			String keyDateFormat = "com.montran.dateFormat";
			dateFormat = UtilPropertyFiles.readPropertiesFile(keyDateFormat);
		} catch (ResourcesException e) {
			e.printStackTrace();
		}
	}

	public List<News> searchNewsByDateResources(List<News> listNews) {
		List<News> result = new ArrayList<News>();
		try {

			SearchDate sDate = new SearchDate("dateBegin", dateFormat, 2);
			SearchDate sDate2 = new SearchDate( "dateEnd", dateFormat, 3);
			SearchChain searchChain = new SearchChain();
			searchChain.addFilter(sDate);
			searchChain.addFilter(sDate2);

			for (News n : listNews) {
				sDate.setDataSource(n.getPublishingDate());
				sDate2.setDataSource(n.getPublishingDate());
				if (searchChain.processSearch()) {
					result.add(n);
				}
			}

		} catch (FilterException e) {
			e.toString();
		}
		return result;
	}

	public List<News> searchNewsByDateAndName(List<News> listNews, Date date,
			String title) {
		List<News> result = new ArrayList<News>();
		try {
			SearchDate sDate = new SearchDate(1, date);
			SearchWord sTitle = new SearchWord(1, title);
			SearchChain searchChain = new SearchChain();
			searchChain.addFilter(sDate);
			searchChain.addFilter(sTitle);

			for (News n : listNews) {
				sDate.setDataSource(n.getPublishingDate());
				sTitle.setDataSource(n.getTitle());
				if (searchChain.processSearch()) {
					result.add(n);
				}
			}

		} catch (FilterException e) {
			e.toString();
		}
		return result;
	}
}
